﻿using Sidvall.Collections;
using System.Linq;

namespace Sidvall.WorkItemManager.Model.DataPortalManagers
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public partial class WorkItemManager<TEntity, TList> : Sidvall.Business.BusinessManagerBase
        where TEntity : global::Sidvall.WorkItemManager.Data.IWorkItem
        where TList : Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItem>
    {
        #region Public Members

        #region DeleteItemByPrimaryKeyAsync

        public async System.Threading.Tasks.Task<int> DeleteItemByPrimaryKeyAsync(long workItemId)
        {
            return await DeleteItemByPrimaryKeyAsync(workItemId, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemByPrimaryKeyAsync(long workItemId, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemId, Sidvall.Data.ComparisonOperator.Equal, workItemId);

            return await DeleteItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region DeleteItemsByPrimaryKeysAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<int> DeleteItemsByPrimaryKeysAsync(System.Collections.Generic.IEnumerable<long> keys)
        {
            return await DeleteItemsByPrimaryKeysAsync(keys, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<int> DeleteItemsByPrimaryKeysAsync(System.Collections.Generic.IEnumerable<long> keys, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if ((keys == null) || (!keys.Any()))
                return 0;
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemId, Sidvall.Data.ComparisonOperator.Equal, keys);

            return await DeleteItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region DeleteItemsByWorkerAsync

        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkerAsync(long workerId)
        {
            return await DeleteItemsByWorkerAsync(workerId, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkerAsync(long workerId, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkerId, Sidvall.Data.ComparisonOperator.Equal, workerId);

            return await DeleteItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkerAsync(System.Collections.Generic.IEnumerable<long> workerIds)
        {
            return await DeleteItemsByWorkerAsync(workerIds, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkerAsync(System.Collections.Generic.IEnumerable<long> workerIds, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkerId, Sidvall.Data.ComparisonOperator.Equal, workerIds);

            return await DeleteItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region DeleteItemsByWorkItemTypeAsync

        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkItemTypeAsync(long workItemTypeId)
        {
            return await DeleteItemsByWorkItemTypeAsync(workItemTypeId, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkItemTypeAsync(long workItemTypeId, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemTypeId, Sidvall.Data.ComparisonOperator.Equal, workItemTypeId);

            return await DeleteItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkItemTypeAsync(System.Collections.Generic.IEnumerable<long> workItemTypeIds)
        {
            return await DeleteItemsByWorkItemTypeAsync(workItemTypeIds, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkItemTypeAsync(System.Collections.Generic.IEnumerable<long> workItemTypeIds, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemTypeId, Sidvall.Data.ComparisonOperator.Equal, workItemTypeIds);

            return await DeleteItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region DeleteItemsByWorkRetryPolicyAsync

        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkRetryPolicyAsync(long workRetryPolicyId)
        {
            return await DeleteItemsByWorkRetryPolicyAsync(workRetryPolicyId, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkRetryPolicyAsync(long workRetryPolicyId, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkRetryPolicyId, Sidvall.Data.ComparisonOperator.Equal, workRetryPolicyId);

            return await DeleteItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkRetryPolicyAsync(System.Collections.Generic.IEnumerable<long> workRetryPolicyIds)
        {
            return await DeleteItemsByWorkRetryPolicyAsync(workRetryPolicyIds, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsByWorkRetryPolicyAsync(System.Collections.Generic.IEnumerable<long> workRetryPolicyIds, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkRetryPolicyId, Sidvall.Data.ComparisonOperator.Equal, workRetryPolicyIds);

            return await DeleteItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region DeleteItemsAsync

        public async System.Threading.Tasks.Task<int> DeleteItemsAsync()
        {
            global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewDeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();

            return await DeleteItemsAsync(criteria, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsAsync(global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria)
        {
            return await DeleteItemsAsync(criteria, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> DeleteItemsAsync(global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            var dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.DeleteItems, strategyContext, criteria, 0);
            dataPortalContext.ObjectParameters.Merge(objectParameters);
            return await DeleteItemsAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeDelete(Sidvall.Business.DataPortalContext<int, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext);
        partial void OnAfterDelete(Sidvall.Business.DataPortalContext<int, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<int> DeleteItemsAsync(Sidvall.Business.DataPortalContext<int, global::Sidvall.Data.DeleteCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeDelete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).DeleteItemsAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterDelete(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return dataPortalContext.ResultValueItem;
        }

        #endregion
        #region GetItemByPrimaryKeyAsync

        public async System.Threading.Tasks.Task<TEntity> GetItemByPrimaryKeyAsync(long workItemId)
        {
            return await GetItemByPrimaryKeyAsync(workItemId, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<TEntity> GetItemByPrimaryKeyAsync(long workItemId, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemId, Sidvall.Data.ComparisonOperator.Equal, workItemId);

            return await GetItemAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByPrimaryKeysAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByPrimaryKeysAsync(System.Collections.Generic.IEnumerable<long> keys)
        {
            return await GetItemsByPrimaryKeysAsync(keys, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByPrimaryKeysAsync(System.Collections.Generic.IEnumerable<long> keys, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if ((keys == null) || (!keys.Any()))
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemId, Sidvall.Data.ComparisonOperator.Equal, keys);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkItemLogsAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemLogsAsync(System.Collections.Generic.IEnumerable<long> workItemIds)
        {
            return await GetItemsByWorkItemLogsAsync(workItemIds, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemLogsAsync(System.Collections.Generic.IEnumerable<long> workItemIds, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemId, Sidvall.Data.ComparisonOperator.Equal, workItemIds);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemLogsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemLog> items)
        {
            return await GetItemsByWorkItemLogsAsync(items, null, null, null).ConfigureAwait(false);
        }
        partial void OnBeforeGetItemsByWorkItemLogs(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemLogsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemLog> items, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (items == null)
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            var ids = (from o in items
                       select o.WorkItemId);
            if (!ids.Any())
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            OnBeforeGetItemsByWorkItemLogs(ids, criteria, strategyContext, objectParameters);

            return await GetItemsByWorkItemLogsAsync(ids, criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkItemSensorsAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemSensorsAsync(System.Collections.Generic.IEnumerable<long> workItemIds)
        {
            return await GetItemsByWorkItemSensorsAsync(workItemIds, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemSensorsAsync(System.Collections.Generic.IEnumerable<long> workItemIds, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemId, Sidvall.Data.ComparisonOperator.Equal, workItemIds);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemSensorsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemSensor> items)
        {
            return await GetItemsByWorkItemSensorsAsync(items, null, null, null).ConfigureAwait(false);
        }
        partial void OnBeforeGetItemsByWorkItemSensors(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemSensorsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemSensor> items, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (items == null)
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            var ids = (from o in items
                       select o.WorkItemId);
            if (!ids.Any())
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            OnBeforeGetItemsByWorkItemSensors(ids, criteria, strategyContext, objectParameters);

            return await GetItemsByWorkItemSensorsAsync(ids, criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkItemWorkConditionsAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemWorkConditionsAsync(System.Collections.Generic.IEnumerable<long> workItemIds)
        {
            return await GetItemsByWorkItemWorkConditionsAsync(workItemIds, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemWorkConditionsAsync(System.Collections.Generic.IEnumerable<long> workItemIds, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemId, Sidvall.Data.ComparisonOperator.Equal, workItemIds);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemWorkConditionsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition> items)
        {
            return await GetItemsByWorkItemWorkConditionsAsync(items, null, null, null).ConfigureAwait(false);
        }
        partial void OnBeforeGetItemsByWorkItemWorkConditions(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemWorkConditionsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition> items, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (items == null)
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            var ids = (from o in items
                       select o.WorkItemId);
            if (!ids.Any())
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            OnBeforeGetItemsByWorkItemWorkConditions(ids, criteria, strategyContext, objectParameters);

            return await GetItemsByWorkItemWorkConditionsAsync(ids, criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkersAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkersAsync(System.Collections.Generic.IEnumerable<long> workerIds)
        {
            return await GetItemsByWorkersAsync(workerIds, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkersAsync(System.Collections.Generic.IEnumerable<long> workerIds, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkerId, Sidvall.Data.ComparisonOperator.Equal, workerIds);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkersAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorker> items)
        {
            return await GetItemsByWorkersAsync(items, null, null, null).ConfigureAwait(false);
        }
        partial void OnBeforeGetItemsByWorkers(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkersAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorker> items, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (items == null)
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            var ids = (from o in items
                       where (o.WorkerId != null)
                       select o.WorkerId.Value);
            if (!ids.Any())
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            OnBeforeGetItemsByWorkers(ids, criteria, strategyContext, objectParameters);

            return await GetItemsByWorkersAsync(ids, criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkerAsync
        
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkerAsync(long workerId)
        {
            return await GetItemsByWorkerAsync(workerId, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkerAsync(long workerId, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkerId, Sidvall.Data.ComparisonOperator.Equal, workerId);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkItemTypesAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemTypesAsync(System.Collections.Generic.IEnumerable<long> workItemTypeIds)
        {
            return await GetItemsByWorkItemTypesAsync(workItemTypeIds, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemTypesAsync(System.Collections.Generic.IEnumerable<long> workItemTypeIds, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemTypeId, Sidvall.Data.ComparisonOperator.Equal, workItemTypeIds);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemTypesAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemType> items)
        {
            return await GetItemsByWorkItemTypesAsync(items, null, null, null).ConfigureAwait(false);
        }
        partial void OnBeforeGetItemsByWorkItemTypes(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemTypesAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemType> items, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (items == null)
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            var ids = (from o in items
                       where (o.WorkItemTypeId != null)
                       select o.WorkItemTypeId.Value);
            if (!ids.Any())
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            OnBeforeGetItemsByWorkItemTypes(ids, criteria, strategyContext, objectParameters);

            return await GetItemsByWorkItemTypesAsync(ids, criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkItemTypeAsync
        
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemTypeAsync(long workItemTypeId)
        {
            return await GetItemsByWorkItemTypeAsync(workItemTypeId, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkItemTypeAsync(long workItemTypeId, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemTypeId, Sidvall.Data.ComparisonOperator.Equal, workItemTypeId);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkRetryPoliciesAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkRetryPoliciesAsync(System.Collections.Generic.IEnumerable<long> workRetryPolicyIds)
        {
            return await GetItemsByWorkRetryPoliciesAsync(workRetryPolicyIds, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkRetryPoliciesAsync(System.Collections.Generic.IEnumerable<long> workRetryPolicyIds, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkRetryPolicyId, Sidvall.Data.ComparisonOperator.Equal, workRetryPolicyIds);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkRetryPoliciesAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkRetryPolicy> items)
        {
            return await GetItemsByWorkRetryPoliciesAsync(items, null, null, null).ConfigureAwait(false);
        }
        partial void OnBeforeGetItemsByWorkRetryPolicies(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkRetryPoliciesAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkRetryPolicy> items, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (items == null)
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            var ids = (from o in items
                       where (o.WorkRetryPolicyId != null)
                       select o.WorkRetryPolicyId.Value);
            if (!ids.Any())
                return (TList)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            OnBeforeGetItemsByWorkRetryPolicies(ids, criteria, strategyContext, objectParameters);

            return await GetItemsByWorkRetryPoliciesAsync(ids, criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsByWorkRetryPolicyAsync
        
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkRetryPolicyAsync(long workRetryPolicyId)
        {
            return await GetItemsByWorkRetryPolicyAsync(workRetryPolicyId, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsByWorkRetryPolicyAsync(long workRetryPolicyId, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            if (criteria.Filter == null)
                criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkRetryPolicyId, Sidvall.Data.ComparisonOperator.Equal, workRetryPolicyId);

            return await GetItemsAsync(criteria, strategyContext, objectParameters).ConfigureAwait(false);
        }

        #endregion
        #region GetItemAsync

        public async System.Threading.Tasks.Task<TEntity> GetItemAsync(global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria)
        {
            return await GetItemAsync(criteria, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<TEntity> GetItemAsync(global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            var dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.GetItems, strategyContext, criteria, (global::Sidvall.WorkItemManager.Data.IWorkItem)null);
            dataPortalContext.ObjectParameters.Merge(objectParameters);
            return await GetItemAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeGetItem(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>> dataPortalContext);
        partial void OnAfterGetItem(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TEntity> GetItemAsync(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeGetItem(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).GetItemAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterGetItem(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return (TEntity)dataPortalContext.ResultValueItem;
        }

        #endregion
        #region GetItemsAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsAsync()
        {
            global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();

            return await GetItemsAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsAsync(global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria)
        {
            return await GetItemsAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsAsync(global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>> dataPortalContext;

            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>();
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.GetItems, strategyContext, criteria, (global::Sidvall.WorkItemManager.Data.IWorkItem)null);
            dataPortalContext.ObjectParameters.Merge(objectParameters);

            return await GetItemsAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeGetItems(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>> dataPortalContext);
        partial void OnAfterGetItems(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> GetItemsAsync(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation>> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeGetItems(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).GetItemsAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterGetItems(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return (TList)dataPortalContext.GetResultValueListProviderResolved();
        }

        #endregion
        #region NewItemAsync

        public async System.Threading.Tasks.Task<TEntity> NewItemAsync(global::Sidvall.Data.CreateCriteria criteria)
        {
            return await NewItemAsync(criteria, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<TEntity> NewItemAsync(global::Sidvall.Data.CreateCriteria criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.CreateCriteria> dataPortalContext;
            
            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.NewItems, strategyContext, criteria, (global::Sidvall.WorkItemManager.Data.IWorkItem)null);
            dataPortalContext.ObjectParameters.Merge(objectParameters);

            return await NewItemAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeNewItem(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.CreateCriteria> dataPortalContext);
        partial void OnAfterNewItem(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.CreateCriteria> dataPortalContext);
        public async System.Threading.Tasks.Task<TEntity> NewItemAsync(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.CreateCriteria> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeNewItem(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).NewItemAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterNewItem(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return (TEntity)dataPortalContext.ResultValueItem;
        }

        #endregion
        #region NewItemsAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> NewItemsAsync()
        {
            global::Sidvall.Data.CreateCriteria criteria;

            criteria = Sidvall.Data.CriteriaManager.NewCreateCriteria();

            return await NewItemsAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> NewItemsAsync(global::Sidvall.Data.CreateCriteria criteria)
        {
            return await NewItemsAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> NewItemsAsync(global::Sidvall.Data.CreateCriteria criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.CreateCriteria> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.NewItems, strategyContext, criteria, (global::Sidvall.WorkItemManager.Data.IWorkItem)null);
            dataPortalContext.ObjectParameters.Merge(objectParameters);

            return await NewItemsAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeNewItems(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.CreateCriteria> dataPortalContext);
        partial void OnAfterNewItems(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.CreateCriteria> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> NewItemsAsync(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, global::Sidvall.Data.CreateCriteria> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeNewItems(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).NewItemsAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterNewItems(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return (TList)dataPortalContext.GetResultValueListProviderResolved();
        }

        #endregion
        #region GetItemCountByWorkerAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountByWorkerAsync(long workerId)
        {
            global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkerId, Sidvall.Data.ComparisonOperator.Equal, workerId);

            return await GetItemCountAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountByWorkerAsync(System.Collections.Generic.IEnumerable<long> workerIds)
        {
            global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkerId, Sidvall.Data.ComparisonOperator.Equal, workerIds);

            return await GetItemCountAsync(criteria, null, null).ConfigureAwait(false);
        }

        #endregion
        #region GetItemCountByWorkItemTypeAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountByWorkItemTypeAsync(long workItemTypeId)
        {
            global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemTypeId, Sidvall.Data.ComparisonOperator.Equal, workItemTypeId);

            return await GetItemCountAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountByWorkItemTypeAsync(System.Collections.Generic.IEnumerable<long> workItemTypeIds)
        {
            global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemTypeId, Sidvall.Data.ComparisonOperator.Equal, workItemTypeIds);

            return await GetItemCountAsync(criteria, null, null).ConfigureAwait(false);
        }

        #endregion
        #region GetItemCountByWorkRetryPolicyAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountByWorkRetryPolicyAsync(long workRetryPolicyId)
        {
            global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkRetryPolicyId, Sidvall.Data.ComparisonOperator.Equal, workRetryPolicyId);

            return await GetItemCountAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountByWorkRetryPolicyAsync(System.Collections.Generic.IEnumerable<long> workRetryPolicyIds)
        {
            global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter = new global::Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            criteria.Filter.AddFilterItem(global::Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkRetryPolicyId, Sidvall.Data.ComparisonOperator.Equal, workRetryPolicyIds);

            return await GetItemCountAsync(criteria, null, null).ConfigureAwait(false);
        }

        #endregion
        #region GetItemCountAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountAsync()
        {
            global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria;

            criteria = Sidvall.Data.CriteriaManager.NewFetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();

            return await GetItemCountAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountAsync(global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria)
        {
            return await GetItemCountAsync(criteria, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountAsync(global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.Business.DataPortalContext<long, global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext;

            if (criteria == null)
                criteria = Sidvall.Data.CriteriaManager.NewFetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>();
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.GetItemsMetadata, strategyContext, criteria, 0L);
            dataPortalContext.ObjectParameters.Merge(objectParameters);

            return await GetItemCountAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeGetItemCount(global::Sidvall.Business.DataPortalContext<long, global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext);
        partial void OnAfterGetItemCount(global::Sidvall.Business.DataPortalContext<long, global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<long> GetItemCountAsync(global::Sidvall.Business.DataPortalContext<long, global::Sidvall.Data.FetchCountCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeGetItemCount(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).GetItemCountAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterGetItemCount(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return dataPortalContext.ResultValueItem;
        }

        #endregion
        #region SaveItemAsync

        public async System.Threading.Tasks.Task<TEntity> SaveItemAsync(TEntity item)
        {
            return await SaveItemAsync(item, null, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<TEntity> SaveItemAsync(TEntity item, Sidvall.Data.SaveMode mode)
        {
            var settings = new Sidvall.Data.SaveSettings()
            {
                Mode = mode
            };
            return await SaveItemAsync(item, settings, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<TEntity> SaveItemAsync(TEntity item, Sidvall.Data.SaveSettings settings)
        {
            return await SaveItemAsync(item, settings, null, null).ConfigureAwait(false);
        }
        partial void OnBeforeSaveItem(global::Sidvall.Data.SaveContext<global::Sidvall.WorkItemManager.Data.IWorkItem> context);
        public async System.Threading.Tasks.Task<TEntity> SaveItemAsync(TEntity item, Sidvall.Data.SaveSettings settings, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            var context = new global::Sidvall.Data.SaveContext<global::Sidvall.WorkItemManager.Data.IWorkItem>()
            {
                Item = item,
                Settings = settings
            };
            OnBeforeSaveItem(context);
            var dataPortalContext = Sidvall.Business.DataPortalManager.NewSaveItemContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, strategyContext, context);
            dataPortalContext.ObjectParameters.Merge(objectParameters);
            return await SaveItemAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeSaveItem(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, Sidvall.Data.SaveContext<global::Sidvall.WorkItemManager.Data.IWorkItem>> dataPortalContext);
        partial void OnAfterSaveItem(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, Sidvall.Data.SaveContext<global::Sidvall.WorkItemManager.Data.IWorkItem>> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TEntity> SaveItemAsync(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, Sidvall.Data.SaveContext<global::Sidvall.WorkItemManager.Data.IWorkItem>> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    if (dataPortalContext.CopyOnSave)
                        dataPortalContext.OperationContext.Item = this.DataPortalManager.GenericEntityFacade.WorkItemManager.Copy(dataPortalContext.OperationContext.Item, Sidvall.Data.CopySettings.Clone());
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeSaveItem(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).SaveItemAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterSaveItem(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return (TEntity)dataPortalContext.ResultValueItem;
        }

        #endregion
        #region SaveItemsAsync

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> SaveItemsAsync(TList items)
        {
            return await SaveItemsAsync(items, null, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> SaveItemsAsync(TList items, Sidvall.Data.SaveMode mode)
        {
            var settings = new Sidvall.Data.SaveSettings()
            {
                Mode = mode
            };
            return await SaveItemsAsync(items, settings, null, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> SaveItemsAsync(TList items, Sidvall.Data.SaveSettings settings)
        {
            return await SaveItemsAsync(items, settings, null, null).ConfigureAwait(false);
        }
        partial void OnBeforeSaveItems(global::Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItem>> context);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> SaveItemsAsync(TList items, Sidvall.Data.SaveSettings settings, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            var context = new Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItem>>()
            {
                Item = items,
                Settings = settings
            };
            OnBeforeSaveItems(context);
            var dataPortalContext = Sidvall.Business.DataPortalManager.NewSaveItemsContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, strategyContext, context);
            dataPortalContext.ObjectParameters.Merge(objectParameters);
            return await SaveItemsAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeSaveItems(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItem>>> dataPortalContext);
        partial void OnAfterSaveItems(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItem>>> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<TList> SaveItemsAsync(global::Sidvall.Business.DataPortalContext<global::Sidvall.WorkItemManager.Data.IWorkItem, Sidvall.Data.SaveContext<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItem>>> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    if (dataPortalContext.CopyOnSave)
                        dataPortalContext.OperationContext.Item = this.DataPortalManager.GenericEntityFacade.WorkItemManager.Copy(dataPortalContext.OperationContext.Item.DataItems, Sidvall.Data.CopySettings.Clone());
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeSaveItems(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).SaveItemsAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterSaveItems(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return (TList)dataPortalContext.GetResultValueListProviderResolved();
        }

        #endregion
        #region UpdateItemAsync

        public async System.Threading.Tasks.Task<bool> UpdateItemAsync(Sidvall.Data.UpdateItemContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> context)
        {
            return await UpdateItemAsync(context, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<bool> UpdateItemAsync(Sidvall.Data.UpdateItemContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> context, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (context == null)
                throw new System.ArgumentNullException(nameof(context));
            var dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.UpdateItems, strategyContext, context, false);
            dataPortalContext.ObjectParameters.Merge(objectParameters);
            return await UpdateItemAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeUpdateItem(global::Sidvall.Business.DataPortalContext<bool, Sidvall.Data.UpdateItemContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext);
        partial void OnAfterUpdateItem(global::Sidvall.Business.DataPortalContext<bool, Sidvall.Data.UpdateItemContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<bool> UpdateItemAsync(global::Sidvall.Business.DataPortalContext<bool, Sidvall.Data.UpdateItemContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeUpdateItem(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).UpdateItemAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterUpdateItem(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return dataPortalContext.ResultValueItem;
        }

        #endregion
        #region UpdateItemsAsync

        public async System.Threading.Tasks.Task<int> UpdateItemsAsync(Sidvall.Data.UpdateItemsContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> context)
        {
            return await UpdateItemsAsync(context, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<int> UpdateItemsAsync(Sidvall.Data.UpdateItemsContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> context, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            if (context == null)
                throw new System.ArgumentNullException(nameof(context));
            var dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.UpdateItems, strategyContext, context, 0);
            dataPortalContext.ObjectParameters.Merge(objectParameters);
            return await UpdateItemsAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeUpdateItems(global::Sidvall.Business.DataPortalContext<int, Sidvall.Data.UpdateItemsContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext);
        partial void OnAfterUpdateItems(global::Sidvall.Business.DataPortalContext<int, Sidvall.Data.UpdateItemsContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public async System.Threading.Tasks.Task<int> UpdateItemsAsync(global::Sidvall.Business.DataPortalContext<int, Sidvall.Data.UpdateItemsContext<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField>> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeUpdateItems(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).UpdateItemsAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterUpdateItems(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return dataPortalContext.ResultValueItem;
        }

        #endregion
        #region ExecuteAsync

        public async System.Threading.Tasks.Task<global::Sidvall.Data.CommandCriteria> ExecuteAsync(string commandId, Sidvall.Data.ParameterCollection parameters)
        {
            global::Sidvall.Data.CommandCriteria criteria;

            criteria = Sidvall.Data.CriteriaManager.NewCommandCriteria(commandId, parameters);

            return await ExecuteAsync(criteria, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<global::Sidvall.Data.CommandCriteria> ExecuteAsync(global::Sidvall.Data.CommandCriteria criteria)
        {
            return await ExecuteAsync(criteria, null, null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<global::Sidvall.Data.CommandCriteria> ExecuteAsync(global::Sidvall.Data.CommandCriteria criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.Business.DataPortalContext<global::Sidvall.Data.CommandCriteria, global::Sidvall.Data.CommandCriteria> dataPortalContext;

            if (criteria == null)
                throw new System.ArgumentNullException(nameof(criteria));
            dataPortalContext = Sidvall.Business.DataPortalManager.NewContext(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, Sidvall.Security.Operations.Execute, strategyContext, criteria);
            dataPortalContext.ObjectParameters.Merge(objectParameters);

            return await ExecuteAsync(dataPortalContext).ConfigureAwait(false);
        }
        partial void OnBeforeExecute(global::Sidvall.Business.DataPortalContext<global::Sidvall.Data.CommandCriteria, global::Sidvall.Data.CommandCriteria> dataPortalContext);
        partial void OnAfterExecute(global::Sidvall.Business.DataPortalContext<global::Sidvall.Data.CommandCriteria, global::Sidvall.Data.CommandCriteria> dataPortalContext);
        public async System.Threading.Tasks.Task<global::Sidvall.Data.CommandCriteria> ExecuteAsync(global::Sidvall.Business.DataPortalContext<global::Sidvall.Data.CommandCriteria, global::Sidvall.Data.CommandCriteria> dataPortalContext)
        {
            if (dataPortalContext == null)
                throw new System.ArgumentNullException(nameof(dataPortalContext));
            do
            {
                try
                {
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvoke(dataPortalContext);
                    OnBeforeExecute(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                    if (dataPortalContext.Result == Sidvall.Business.DataPortalResult.None)
                    {
                        await GetDataPortal(dataPortalContext).ExecuteAsync(dataPortalContext).ConfigureAwait(false);
                    }
                    OnAfterExecute(dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalInvokeComplete(dataPortalContext);
                    await dataPortalContext.DoTaskAsync().ConfigureAwait(false);
                }
                catch (System.Exception ex)
                {
                    dataPortalContext.Exception = Sidvall.Business.DataPortalManager.GetDataPortalException(ex, dataPortalContext);
                    global::Sidvall.WorkItemManager.Model.DataPortalEventManager.DataPortalException(dataPortalContext);
                    await dataPortalContext.DoTaskAsync(!dataPortalContext.Retry).ConfigureAwait(false);
                }
            } while (dataPortalContext.DoRetry());
            return dataPortalContext.ResultValueItem;
        }

        #endregion
        #region LoadWorkItemLogsAsync

        partial void OnBeforeLoadWorkItemLogsAsync(ref System.Threading.Tasks.Task<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemLog>> task, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemLogField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemLogRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkItemLogsAsync(global::Sidvall.WorkItemManager.Data.IWorkItem item, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemLogField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemLogRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemLog> bo;
            System.Threading.Tasks.Task<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemLog>> task;

            if (item.DataRowStateIsNew)
            {
                if ((item.WorkItemLogs == null) || (refresh))
                {
                    bo = (Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemLog>)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemLog);
                    item.SetWorkItemLogs(bo);
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemLogs", raisePropertyChanged, false);
                }
            }
            else
            {
                if ((item.WorkItemLogs == null) || (refresh))
                {
                    if (item.WorkItemId != null)
                    {
                        task = null;
                        OnBeforeLoadWorkItemLogsAsync(ref task, criteria, strategyContext, objectParameters);
                        if (task != null)
                            bo = await task.ConfigureAwait(false);
                        else
                            bo = await this.DataPortalManager.GenericDataPortal.WorkItemLogManager.GetItemsByWorkItemAsync(item.WorkItemId.Value, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                        item.SetWorkItemLogs(bo);
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemLogs", raisePropertyChanged, false);
                    }
                }
            }
        }
        partial void OnBeforeLoadWorkItemLogs(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemLogField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemLogRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkItemLogsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> items, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemLogField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemLogRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            System.Collections.ObjectModel.Collection<global::Sidvall.WorkItemManager.Data.IWorkItemLog> relatedList;

            if (items == null)
                return;

            var selectedItems = GetItemsWithUnloadedRelation(items, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkItemLogs, refresh, criteria);
            var relatedItems = await this.DataPortalManager.GenericDataPortal.WorkItemLogManager.GetItemsByWorkItemsAsync(selectedItems, criteria, strategyContext, objectParameters).ConfigureAwait(false);
            var relatedListIndex = relatedItems.DataItems.ToDictionaryCollection(o => o.WorkItemId);

            foreach (var selectedItem in selectedItems)
            {
                if ((relatedListIndex.TryGetValue(selectedItem.WorkItemId.Value, out relatedList)) && (relatedList != null))
                    selectedItem.SetWorkItemLogs(this.DataPortalManager.GenericEntityFacade.WorkItemLogManager.Copy(relatedList, new Sidvall.Data.CopySettings(Sidvall.Business.FieldCopyOptions.All, Sidvall.Data.DataRowContextOption.State, null)));
                else
                    selectedItem.SetWorkItemLogs((Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemLog>)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemLog));
                Sidvall.Data.EntityManager.OnPropertyChanged(selectedItem, "WorkItemLogs", raisePropertyChanged, false);
            }
        }

        #endregion
        #region LoadWorkItemSensorsAsync

        partial void OnBeforeLoadWorkItemSensorsAsync(ref System.Threading.Tasks.Task<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemSensor>> task, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemSensorField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemSensorRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkItemSensorsAsync(global::Sidvall.WorkItemManager.Data.IWorkItem item, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemSensorField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemSensorRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemSensor> bo;
            System.Threading.Tasks.Task<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemSensor>> task;

            if (item.DataRowStateIsNew)
            {
                if ((item.WorkItemSensors == null) || (refresh))
                {
                    bo = (Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemSensor>)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemSensor);
                    item.SetWorkItemSensors(bo);
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemSensors", raisePropertyChanged, false);
                }
            }
            else
            {
                if ((item.WorkItemSensors == null) || (refresh))
                {
                    if (item.WorkItemId != null)
                    {
                        task = null;
                        OnBeforeLoadWorkItemSensorsAsync(ref task, criteria, strategyContext, objectParameters);
                        if (task != null)
                            bo = await task.ConfigureAwait(false);
                        else
                            bo = await this.DataPortalManager.GenericDataPortal.WorkItemSensorManager.GetItemsByWorkItemAsync(item.WorkItemId.Value, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                        item.SetWorkItemSensors(bo);
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemSensors", raisePropertyChanged, false);
                    }
                }
            }
        }
        partial void OnBeforeLoadWorkItemSensors(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemSensorField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemSensorRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkItemSensorsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> items, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemSensorField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemSensorRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            System.Collections.ObjectModel.Collection<global::Sidvall.WorkItemManager.Data.IWorkItemSensor> relatedList;

            if (items == null)
                return;

            var selectedItems = GetItemsWithUnloadedRelation(items, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkItemSensors, refresh, criteria);
            var relatedItems = await this.DataPortalManager.GenericDataPortal.WorkItemSensorManager.GetItemsByWorkItemsAsync(selectedItems, criteria, strategyContext, objectParameters).ConfigureAwait(false);
            var relatedListIndex = relatedItems.DataItems.ToDictionaryCollection(o => o.WorkItemId);

            foreach (var selectedItem in selectedItems)
            {
                if ((relatedListIndex.TryGetValue(selectedItem.WorkItemId.Value, out relatedList)) && (relatedList != null))
                    selectedItem.SetWorkItemSensors(this.DataPortalManager.GenericEntityFacade.WorkItemSensorManager.Copy(relatedList, new Sidvall.Data.CopySettings(Sidvall.Business.FieldCopyOptions.All, Sidvall.Data.DataRowContextOption.State, null)));
                else
                    selectedItem.SetWorkItemSensors((Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemSensor>)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemSensor));
                Sidvall.Data.EntityManager.OnPropertyChanged(selectedItem, "WorkItemSensors", raisePropertyChanged, false);
            }
        }

        #endregion
        #region LoadWorkItemWorkConditionsAsync

        partial void OnBeforeLoadWorkItemWorkConditionsAsync(ref System.Threading.Tasks.Task<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition>> task, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemWorkConditionField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemWorkConditionRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkItemWorkConditionsAsync(global::Sidvall.WorkItemManager.Data.IWorkItem item, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemWorkConditionField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemWorkConditionRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition> bo;
            System.Threading.Tasks.Task<Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition>> task;

            if (item.DataRowStateIsNew)
            {
                if ((item.WorkItemWorkConditions == null) || (refresh))
                {
                    bo = (Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition>)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemWorkCondition);
                    item.SetWorkItemWorkConditions(bo);
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemWorkConditions", raisePropertyChanged, false);
                }
            }
            else
            {
                if ((item.WorkItemWorkConditions == null) || (refresh))
                {
                    if (item.WorkItemId != null)
                    {
                        task = null;
                        OnBeforeLoadWorkItemWorkConditionsAsync(ref task, criteria, strategyContext, objectParameters);
                        if (task != null)
                            bo = await task.ConfigureAwait(false);
                        else
                            bo = await this.DataPortalManager.GenericDataPortal.WorkItemWorkConditionManager.GetItemsByWorkItemAsync(item.WorkItemId.Value, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                        item.SetWorkItemWorkConditions(bo);
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemWorkConditions", raisePropertyChanged, false);
                    }
                }
            }
        }
        partial void OnBeforeLoadWorkItemWorkConditions(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemWorkConditionField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemWorkConditionRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkItemWorkConditionsAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> items, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemWorkConditionField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemWorkConditionRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            System.Collections.ObjectModel.Collection<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition> relatedList;

            if (items == null)
                return;

            var selectedItems = GetItemsWithUnloadedRelation(items, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkItemWorkConditions, refresh, criteria);
            var relatedItems = await this.DataPortalManager.GenericDataPortal.WorkItemWorkConditionManager.GetItemsByWorkItemsAsync(selectedItems, criteria, strategyContext, objectParameters).ConfigureAwait(false);
            var relatedListIndex = relatedItems.DataItems.ToDictionaryCollection(o => o.WorkItemId);

            foreach (var selectedItem in selectedItems)
            {
                if ((relatedListIndex.TryGetValue(selectedItem.WorkItemId.Value, out relatedList)) && (relatedList != null))
                    selectedItem.SetWorkItemWorkConditions(this.DataPortalManager.GenericEntityFacade.WorkItemWorkConditionManager.Copy(relatedList, new Sidvall.Data.CopySettings(Sidvall.Business.FieldCopyOptions.All, Sidvall.Data.DataRowContextOption.State, null)));
                else
                    selectedItem.SetWorkItemWorkConditions((Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition>)this.DataPortalManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemWorkCondition));
                Sidvall.Data.EntityManager.OnPropertyChanged(selectedItem, "WorkItemWorkConditions", raisePropertyChanged, false);
            }
        }

        #endregion
        #region LoadWorkersAsync

        partial void OnBeforeLoadWorkerAsync(ref System.Threading.Tasks.Task<global::Sidvall.WorkItemManager.Data.IWorker> task, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkerField, global::Sidvall.WorkItemManager.Data.Relations.WorkerRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkerAsync(global::Sidvall.WorkItemManager.Data.IWorkItem item, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkerField, global::Sidvall.WorkItemManager.Data.Relations.WorkerRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.WorkItemManager.Data.IWorker bo;
            System.Threading.Tasks.Task<global::Sidvall.WorkItemManager.Data.IWorker> task;

            if (item.DataRowStateIsNew)
            {
                if ((item.Worker == null) || (refresh))
                {
                    task = null;
                    OnBeforeLoadWorkerAsync(ref task, criteria, strategyContext, objectParameters);
                    if (task != null)
                        bo = await task.ConfigureAwait(false);
                    else
                        bo = await this.DataPortalManager.GenericDataPortal.WorkerManager.GetItemByPrimaryKeyAsync(item.WorkerId, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                    item.Worker = bo;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Worker", raisePropertyChanged, false);
                }
            }
            else
            {
                if ((item.Worker == null) || (refresh))
                {
                    task = null;
                    OnBeforeLoadWorkerAsync(ref task, criteria, strategyContext, objectParameters);
                    if (task != null)
                        bo = await task.ConfigureAwait(false);
                    else
                        bo = await this.DataPortalManager.GenericDataPortal.WorkerManager.GetItemByPrimaryKeyAsync(item.WorkerId, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                    item.Worker = bo;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Worker", raisePropertyChanged, false);
                }
            }
        }
        partial void OnBeforeLoadWorker(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkerField, global::Sidvall.WorkItemManager.Data.Relations.WorkerRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkersAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> items, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkerField, global::Sidvall.WorkItemManager.Data.Relations.WorkerRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.WorkItemManager.Data.IWorker parent;

            if (items == null)
                return;

            var selectedItems = GetItemsWithUnloadedRelation(items, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.Worker, refresh, criteria);
            var relatedItems = await this.DataPortalManager.GenericDataPortal.WorkerManager.GetItemsByWorkItemsAsync(selectedItems, criteria, strategyContext, objectParameters).ConfigureAwait(false);
            var relatedListIndex = relatedItems.DataItems.ToDictionary(o => o.WorkerId.Value, true);

            foreach (var selectedItem in selectedItems)
            {
                if (relatedListIndex.TryGetValue(selectedItem.WorkerId, out parent))
                {
                    if (parent != null)
                        selectedItem.Worker = this.DataPortalManager.GenericEntityFacade.WorkerManager.Copy(parent, new Sidvall.Data.CopySettings(Sidvall.Business.FieldCopyOptions.All, Sidvall.Data.DataRowContextOption.State, null));
                    else
                        selectedItem.Worker = null;
                    Sidvall.Data.EntityManager.OnPropertyChanged(selectedItem, "Worker", raisePropertyChanged, false);
                }
            }
        }

        #endregion
        #region LoadWorkItemTypesAsync

        partial void OnBeforeLoadWorkItemTypeAsync(ref System.Threading.Tasks.Task<global::Sidvall.WorkItemManager.Data.IWorkItemType> task, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemTypeField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemTypeRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkItemTypeAsync(global::Sidvall.WorkItemManager.Data.IWorkItem item, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemTypeField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemTypeRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.WorkItemManager.Data.IWorkItemType bo;
            System.Threading.Tasks.Task<global::Sidvall.WorkItemManager.Data.IWorkItemType> task;

            if (item.DataRowStateIsNew)
            {
                if ((item.WorkItemType == null) || (refresh))
                {
                    if (item.WorkItemTypeId != null)
                    {
                        task = null;
                        OnBeforeLoadWorkItemTypeAsync(ref task, criteria, strategyContext, objectParameters);
                        if (task != null)
                            bo = await task.ConfigureAwait(false);
                        else
                            bo = await this.DataPortalManager.GenericDataPortal.WorkItemTypeManager.GetItemByPrimaryKeyAsync(item.WorkItemTypeId.Value, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                        item.WorkItemType = bo;
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemType", raisePropertyChanged, false);
                    }
                    else
                    {
                        bo = (global::Sidvall.WorkItemManager.Data.IWorkItemType)this.DataPortalManager.ResourceFactory.CreateItem(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItemType, null);
                        item.WorkItemType = bo;
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemType", raisePropertyChanged, false);
                    }
                }
            }
            else
            {
                if ((item.WorkItemType == null) || (refresh))
                {
                    if (item.WorkItemTypeId != null)
                    {
                        task = null;
                        OnBeforeLoadWorkItemTypeAsync(ref task, criteria, strategyContext, objectParameters);
                        if (task != null)
                            bo = await task.ConfigureAwait(false);
                        else
                            bo = await this.DataPortalManager.GenericDataPortal.WorkItemTypeManager.GetItemByPrimaryKeyAsync(item.WorkItemTypeId.Value, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                        item.WorkItemType = bo;
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemType", raisePropertyChanged, false);
                    }
                }
            }
        }
        partial void OnBeforeLoadWorkItemType(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemTypeField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemTypeRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkItemTypesAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> items, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemTypeField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemTypeRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.WorkItemManager.Data.IWorkItemType parent;

            if (items == null)
                return;

            var selectedItems = GetItemsWithUnloadedRelation(items, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkItemType, refresh, criteria);
            var relatedItems = await this.DataPortalManager.GenericDataPortal.WorkItemTypeManager.GetItemsByWorkItemsAsync(selectedItems, criteria, strategyContext, objectParameters).ConfigureAwait(false);
            var relatedListIndex = relatedItems.DataItems.ToDictionary(o => o.WorkItemTypeId.Value, true);

            foreach (var selectedItem in selectedItems)
            {
                if (relatedListIndex.TryGetValue(selectedItem.WorkItemTypeId.Value, out parent))
                {
                    if (parent != null)
                        selectedItem.WorkItemType = this.DataPortalManager.GenericEntityFacade.WorkItemTypeManager.Copy(parent, new Sidvall.Data.CopySettings(Sidvall.Business.FieldCopyOptions.All, Sidvall.Data.DataRowContextOption.State, null));
                    else
                        selectedItem.WorkItemType = null;
                    Sidvall.Data.EntityManager.OnPropertyChanged(selectedItem, "WorkItemType", raisePropertyChanged, false);
                }
            }
        }

        #endregion
        #region LoadWorkRetryPoliciesAsync

        partial void OnBeforeLoadWorkRetryPolicyAsync(ref System.Threading.Tasks.Task<global::Sidvall.WorkItemManager.Data.IWorkRetryPolicy> task, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkRetryPolicyField, global::Sidvall.WorkItemManager.Data.Relations.WorkRetryPolicyRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkRetryPolicyAsync(global::Sidvall.WorkItemManager.Data.IWorkItem item, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkRetryPolicyField, global::Sidvall.WorkItemManager.Data.Relations.WorkRetryPolicyRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.WorkItemManager.Data.IWorkRetryPolicy bo;
            System.Threading.Tasks.Task<global::Sidvall.WorkItemManager.Data.IWorkRetryPolicy> task;

            if (item.DataRowStateIsNew)
            {
                if ((item.WorkRetryPolicy == null) || (refresh))
                {
                    if (item.WorkRetryPolicyId != null)
                    {
                        task = null;
                        OnBeforeLoadWorkRetryPolicyAsync(ref task, criteria, strategyContext, objectParameters);
                        if (task != null)
                            bo = await task.ConfigureAwait(false);
                        else
                            bo = await this.DataPortalManager.GenericDataPortal.WorkRetryPolicyManager.GetItemByPrimaryKeyAsync(item.WorkRetryPolicyId.Value, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                        item.WorkRetryPolicy = bo;
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkRetryPolicy", raisePropertyChanged, false);
                    }
                    else
                    {
                        bo = (global::Sidvall.WorkItemManager.Data.IWorkRetryPolicy)this.DataPortalManager.ResourceFactory.CreateItem(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkRetryPolicy, null);
                        item.WorkRetryPolicy = bo;
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkRetryPolicy", raisePropertyChanged, false);
                    }
                }
            }
            else
            {
                if ((item.WorkRetryPolicy == null) || (refresh))
                {
                    if (item.WorkRetryPolicyId != null)
                    {
                        task = null;
                        OnBeforeLoadWorkRetryPolicyAsync(ref task, criteria, strategyContext, objectParameters);
                        if (task != null)
                            bo = await task.ConfigureAwait(false);
                        else
                            bo = await this.DataPortalManager.GenericDataPortal.WorkRetryPolicyManager.GetItemByPrimaryKeyAsync(item.WorkRetryPolicyId.Value, criteria, strategyContext, objectParameters).ConfigureAwait(false);
                        item.WorkRetryPolicy = bo;
                        Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkRetryPolicy", raisePropertyChanged, false);
                    }
                }
            }
        }
        partial void OnBeforeLoadWorkRetryPolicy(System.Collections.Generic.IEnumerable<long> ids, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkRetryPolicyField, global::Sidvall.WorkItemManager.Data.Relations.WorkRetryPolicyRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters);
        public async System.Threading.Tasks.Task LoadWorkRetryPoliciesAsync(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> items, bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkRetryPolicyField, global::Sidvall.WorkItemManager.Data.Relations.WorkRetryPolicyRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            global::Sidvall.WorkItemManager.Data.IWorkRetryPolicy parent;

            if (items == null)
                return;

            var selectedItems = GetItemsWithUnloadedRelation(items, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkRetryPolicy, refresh, criteria);
            var relatedItems = await this.DataPortalManager.GenericDataPortal.WorkRetryPolicyManager.GetItemsByWorkItemsAsync(selectedItems, criteria, strategyContext, objectParameters).ConfigureAwait(false);
            var relatedListIndex = relatedItems.DataItems.ToDictionary(o => o.WorkRetryPolicyId.Value, true);

            foreach (var selectedItem in selectedItems)
            {
                if (relatedListIndex.TryGetValue(selectedItem.WorkRetryPolicyId.Value, out parent))
                {
                    if (parent != null)
                        selectedItem.WorkRetryPolicy = this.DataPortalManager.GenericEntityFacade.WorkRetryPolicyManager.Copy(parent, new Sidvall.Data.CopySettings(Sidvall.Business.FieldCopyOptions.All, Sidvall.Data.DataRowContextOption.State, null));
                    else
                        selectedItem.WorkRetryPolicy = null;
                    Sidvall.Data.EntityManager.OnPropertyChanged(selectedItem, "WorkRetryPolicy", raisePropertyChanged, false);
                }
            }
        }

        #endregion
        #region GetDataPortal

        protected global::Sidvall.Business.IDataPortal GetDataPortal(global::Sidvall.Business.IDataPortalContext dataPortalContext)
        {
            if (this.AlwaysUseLocalDataPortal)
                return this.DataPortalManager.LocalDataPortal;
            if (global::Sidvall.WorkItemManager.SystemContext.Current.DataPortalStrategy.RunLocal(dataPortalContext))
                return this.DataPortalManager.LocalDataPortal;
            else
                return this.DataPortalManager.RemoteDataPortal;
        }

        #endregion

        #endregion
        #region Private Members

        private global::Sidvall.WorkItemManager.Model.IDataPortalManager DataPortalManager { get; set; }
        private bool AlwaysUseLocalDataPortal { get; set; }
        #region GetItemsWithUnloadedRelation

        static partial void OnAfterGetItemsWithUnloadedRelation(ref System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> selectedItems, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation relation, Sidvall.Data.ICriteria criteria);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "criteria"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        internal static System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> GetItemsWithUnloadedRelation(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> items, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation relation, bool refresh, Sidvall.Data.ICriteria criteria)
        {
            System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> result;

            if (items == null)
                return null;
            switch (relation)
            {
                case global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkItemLogs:
                    result = from o in items
                             where (o.WorkItemId != null) && ((refresh) || (o.WorkItemLogs == null))
                             select o;
                    OnAfterGetItemsWithUnloadedRelation(ref result, relation, criteria);
                    return result;
                case global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkItemSensors:
                    result = from o in items
                             where (o.WorkItemId != null) && ((refresh) || (o.WorkItemSensors == null))
                             select o;
                    OnAfterGetItemsWithUnloadedRelation(ref result, relation, criteria);
                    return result;
                case global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkItemWorkConditions:
                    result = from o in items
                             where (o.WorkItemId != null) && ((refresh) || (o.WorkItemWorkConditions == null))
                             select o;
                    OnAfterGetItemsWithUnloadedRelation(ref result, relation, criteria);
                    return result;
                case global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.Worker:
                    result = from o in items
                             where ((refresh) || (o.Worker == null))
                             select o;
                    OnAfterGetItemsWithUnloadedRelation(ref result, relation, criteria);
                    return result;
                case global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkItemType:
                    result = from o in items
                             where (o.WorkItemTypeId != null) && ((refresh) || (o.WorkItemType == null))
                             select o;
                    OnAfterGetItemsWithUnloadedRelation(ref result, relation, criteria);
                    return result;
                case global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation.WorkRetryPolicy:
                    result = from o in items
                             where (o.WorkRetryPolicyId != null) && ((refresh) || (o.WorkRetryPolicy == null))
                             select o;
                    OnAfterGetItemsWithUnloadedRelation(ref result, relation, criteria);
                    return result;
                default:
                    return null;
            }
        }

        #endregion

        #endregion
        #region Constructors

        public WorkItemManager(global::Sidvall.WorkItemManager.Model.IDataPortalManager dataPortalManager, bool alwaysUseLocalDataPortal)
        {
            this.DataPortalManager = dataPortalManager;
            this.AlwaysUseLocalDataPortal = alwaysUseLocalDataPortal;
        }

        #endregion
    }
}